package com.riko.comic.core.config;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.logging.log4j2.Log4j2Impl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 配置核心Mybatis核心工厂
 *
 * @author riko
 * @since 2021/10/29
 */
public class MyBatisPlusConfig {

    @Bean(name="sqlSessionFactoryBean")
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();

        factoryBean.setTypeAliasesPackage("com.riko.**.bean.*");

        //设置日志
        MybatisConfiguration mybatisConfiguration = new MybatisConfiguration();
        mybatisConfiguration.setLogImpl(Log4j2Impl.class);
        factoryBean.setConfiguration(mybatisConfiguration);

        //设置数据源
        factoryBean.setDataSource(dataSource);


        //设置mapper.xml地址
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/**/*Mapper.xml"));

        return factoryBean.getObject();
    }
}
